package com.woniuxy.microretailserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microretailserver.bean.*;
import com.woniuxy.microretailserver.mapper.GoodsPayMapper;
import com.woniuxy.microretailserver.mapper.GoodsReturnInfoMapper;
import com.woniuxy.microretailserver.mapper.OrderGoodsMapper;
import com.woniuxy.microretailserver.service.IGoodsReturnInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author chenhu
 * 新增退货订单业务层实现类
 */
@Transactional(readOnly = true)
@Service
public class GoodsReturnInfoServiceImpl implements IGoodsReturnInfoService {

    @Resource
    private GoodsReturnInfoMapper goodsReturnInfoMapper;
    @Resource
    private OrderGoodsMapper orderGoodsMapper;
    @Resource
    private GoodsPayMapper goodsPayMapper;

    /**
     * 新增退货订单信息
     * @param orderGoodsBean
     * @param returnPayInfoBean
     * @param goodsReturnInfoBean
     * @return
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int saveGoodsReturnInfo(GoodsReturnInfoBean goodsReturnInfoBean, List<OrderGoodsBean> orderGoodsBean, ReturnPayInfoBean returnPayInfoBean) {
        goodsReturnInfoMapper.saveGoodsReturnInfo(goodsReturnInfoBean);
        orderGoodsBean.forEach(e->{
            e.setGoodsReturnInfoBean(goodsReturnInfoBean);
            orderGoodsMapper.saveOrderReturnGoods(e);
        });
        goodsPayMapper.saveGoodsReturnPay(returnPayInfoBean);
        return 0;
    }

    /**
     * 分页查询商品退货订单
     * @param request
     * @param goodsReturnInfoBean
     * @return
     */
    @Override
    public PageInfo<GoodsReturnInfoBean> findAllGoodsReturn(PageRequest request, GoodsReturnInfoBean goodsReturnInfoBean) {
        return PageHelper.startPage(request.getPageNum(),request.getLimit()).
                doSelectPageInfo(()->goodsReturnInfoMapper.findAllGoodsReturn(request, goodsReturnInfoBean));
    }

    /**
     * 查询退货订单详细信息
     * @param orderNo
     * @return
     */
    @Override
    public HashMap<String, Object> findAllReturn(String orderNo) {
        GoodsReturnInfoBean byReturnOrderNo = goodsReturnInfoMapper.findByReturnOrderNo(orderNo);//查询退货订单
        List<OrderGoodsBean> byIdReturnOrder = orderGoodsMapper.findByIdReturnOrder(byReturnOrderNo.getId());//查询商品信息
        ReturnPayInfoBean byIdReturnPay = goodsPayMapper.findByIdReturnPay(byReturnOrderNo.getId());//查询支付信息
        HashMap<String, Object> objectObjectHashMap = new HashMap<String, Object>();
        objectObjectHashMap.put("returnOrderNo",byReturnOrderNo);//查询退货订单
        objectObjectHashMap.put("returnOrder",byIdReturnOrder);//查询商品信息
        objectObjectHashMap.put("returnPay",byIdReturnPay);//查询支付信息
        return objectObjectHashMap;
    }

    /**
     * 导出退货订单数据
     * @return
     */
    @Override
    public List<GoodsReturnInfoBean> findAll() {
        return goodsReturnInfoMapper.findByAllReturn();
    }
}
